package com.woniu.smart.controller;


import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.CarpayDTO;
import com.woniu.smart.entity.CarType;
import com.woniu.smart.entity.Carpay;
import com.woniu.smart.service.CarTypeService;
import com.woniu.smart.service.CarpayService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.CarpayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：mmzs
 * @description：TODO
 * @date ：2022/10/21 15:10
 */
@RestController
@RequestMapping("carpays")
public class CarpayController {
    @Autowired
    private CarpayService carpayService;
    @Autowired
    private CarTypeService carTypeService;
    @PostMapping()
    public Result selAll(@RequestBody CarpayDTO carpayDTO){
        List<CarpayVO> CarpayVOS = carpayService.selectAll(carpayDTO);
        PageInfo<CarpayVO> carpayVOPageInfo = new PageInfo<>(CarpayVOS);
        List<CarType> carTypes = carTypeService.selectAllType();
        return Result.ok().data("carpayList",carpayVOPageInfo).data("carTypes",carTypes);
    }
    @DeleteMapping("{id}")
    public Result delById(@PathVariable("id") Long id){
        int i = carpayService.deleteByPrimaryKey(id);
        return Result.ok();
    }
    //批量删除
    @PostMapping("delByIds")
    public Result delByIds(@RequestBody Long[] ids){
        for (Long i0:ids) {
            int i1 = carpayService.deleteByPrimaryKey(i0);
        }
        return Result.ok();
    }
    //添加缴费记录
    //但是点击添加时，不只车辆缴费表需要添加记录，账单表也需要添加记录
    @PostMapping("add")
    public Result add(@RequestBody CarpayVO carpayVO){
        int i = carpayService.insertCarpaySelective(carpayVO);
        if(i==2){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    //根据车牌号查找车辆信息及车主信息
    @PostMapping("{carNum}")
    public Result selCarpayByCarNum(@PathVariable String carNum){
        CarpayVO carpayVO = carpayService.selectByCarNum(carNum);
        if(carpayVO!=null){
            return Result.ok().data("carpayVO",carpayVO);
        }else {
            return Result.error();
        }
    }

    /**
     * 查找车辆相关信息
     * @param id
     * @return
     */
    @PostMapping("sel/{id}")
    public Result selById(@PathVariable("id") Long id){
        CarpayVO carpayVO = carpayService.selectByCarpayId(id);
        if(carpayVO!=null){
            return Result.ok().data("carpayVO",carpayVO);
        }else {
            return Result.error();
        }
    }

}
